package com.excilys.sae.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.excilys.sae.entity.UserDetails;

/**
 * 
 * Retrieves information for an user via secu-admin database
 * 
 * @author yaziz, gferrand
 *
 */
@Repository
public class UserDetailsDAO extends JdbcTemplate implements IUserDetailsDAO {
		
	@Autowired
	private DataSource dataSource;
	
	
	public List<UserDetails> getAllUserDetails(){
		
		String query = "SELECT * FROM user_details";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		List<UserDetails> userDetailsList = jdbcTemplate.query(query, new RowMapper<UserDetails>() {
			@Override
			public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
				UserDetails userDetails = new UserDetails();
				if(rs.next()) {
					RowMapperUserDetailsMatcher(rs, userDetails);
				}
				return userDetails;				
			}
		});
				
		return userDetailsList;
	}

	public UserDetails getUserDetailsByUsername(String username){
		
		String query = "SELECT * FROM user_details WHERE username = ?";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		
		UserDetails userDetails = new UserDetails();
		
		try{
				userDetails = jdbcTemplate.queryForObject(query, new Object[]{new String(username)}, new RowMapper<UserDetails>() {
				@Override
				public UserDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
					UserDetails userDetails = new UserDetails();
					RowMapperUserDetailsMatcher(rs, userDetails);
					return userDetails;				
				}
			});
		} catch (EmptyResultDataAccessException e){
			System.out.println("ERREUR : la requête n'a retourné aucun résultat : " + e.toString());
			
		}
		return userDetails;
	}
	
	public void RowMapperUserDetailsMatcher(ResultSet resultSet, UserDetails userDetails) throws SQLException{
		userDetails.setId(resultSet.getLong("id"));
		userDetails.setFirstName(resultSet.getString("firstName"));
		userDetails.setLastName(resultSet.getString("lastName"));
		userDetails.setPhone(resultSet.getString("phone"));
		userDetails.setMobile(resultSet.getString("mobile"));
		userDetails.setEmail1(resultSet.getString("email1"));
		userDetails.setEmail2(resultSet.getString("email2"));
		userDetails.setAddress(resultSet.getString("address"));
		userDetails.setCity(resultSet.getString("city"));
		userDetails.setKeyActivation(resultSet.getString("keyActivation"));
		userDetails.setDateSubscription(resultSet.getDate("dateSubscription"));
		userDetails.setSchoolSubscription(resultSet.getString("schoolSubscription"));
		userDetails.setPromotionSubscription(resultSet.getString("promotionSubscription"));
		userDetails.setCompanySubscription(resultSet.getString("companySubscription"));
		userDetails.setFonctionSubscription(resultSet.getString("fonctionSubscription"));
		userDetails.setUserName(resultSet.getString("username"));
		userDetails.setZipCode(resultSet.getString("zipCode"));
		userDetails.setDateOfBirth(resultSet.getDate("dateOfBirth"));
		userDetails.setLanguage(resultSet.getString("userLanguage"));
	}
	
}
